package com.company.project.modular.standard.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.modular.system.service.IStandardService;
import com.company.project.modular.system.model.Standard;
import com.company.project.modular.system.warpper.StandardWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 标准物质查询控制器
 *
 * @author fengshuonan
 * @Date 2018-10-30 15:40:39
 */
@Controller
@RequestMapping("/standard")
public class StandardController extends BaseController {

    private String PREFIX = "/standard/standard/";

    @Reference
    private IStandardService standardService;

    //
    /**
     * 跳转到标准物质查询首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "standard.html";
    }

    /**
     * 跳转到添加标准物质查询
     */
    @RequestMapping("/standard_add")
    public String standardAdd() {
        return PREFIX + "upload.html";
       /* return PREFIX + "standard_add.html";*/
    }

    /**
     * 跳转到上传文件页面
     */
    @RequestMapping("/standard_addDoc/{standardId}")
    public String standardAddDoc(@PathVariable Integer standardId, Model model) {
        Standard standard = standardService.selectById(standardId);
        model.addAttribute("item",standard);
        LogObjectHolder.me().set(standard);
        return PREFIX + "addDoc.html";
    }
    /**
     * 跳转到打印条形码页面
     */
    @RequestMapping("/openStandardJsBarCode/{standardId}")
    public String openStandardJsBarCode(@PathVariable Integer standardId, Model model) {
        Standard standard = standardService.selectById(standardId);
        model.addAttribute("item",standard);
        LogObjectHolder.me().set(standard);
        /*return PREFIX + "jsBarCode.html";*/
        return PREFIX + "jqueryPrint.html";
    }
    /**
     * 跳转到修改标准物质查询
     */
    @RequestMapping("/standard_update/{standardId}")
    public String standardUpdate(@PathVariable Integer standardId, Model model) {
        Standard standard = standardService.selectById(standardId);
        model.addAttribute("item",standard);
        LogObjectHolder.me().set(standard);
        return PREFIX + "standard_edit.html";
    }

    /**
     * 获取标准物质查询列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> standard = standardService.list();
        return new StandardWarpper(standard).warp();
    }
    /**
     * 获取标准物质查询列表
     */
    @RequestMapping(value = "/listdoc")
    @ResponseBody
    public Object listdoc(String condition) {
        List<Map<String, Object>> standard=standardService.selectMaps(null);
        new StandardWarpper(standard).warp();
        return PREFIX + "standard.html";
    }
    /**
     * 新增标准物质查询
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Standard standard, BindingResult bindingResult){
       /*保存StandardId*/
        //得到long类型当前时间
        long l = System.currentTimeMillis(); //new日期对象
        Date date = new Date(l); //转换提日期输出格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String createdate = dateFormat.format(date);
        standard.setStandardId(createdate);
        /*保存StandardLabel*/
        String maxNumber = String.valueOf(standardService.findByMaxNumber().get(0).getStandardLabel());
        if("null".equals(maxNumber)){
            maxNumber = null;
            int a = 50001;
            String number = String.valueOf(a);
            standard.setStandardLabel(number);
        }else {
            int a = Integer.valueOf(maxNumber)+1;
            String number = String.valueOf(a);
            standard.setStandardLabel(number);
        }
        /*保存StandardCode*/
        String standardCode =  standard.getStandardId() + standard.getStandardLabel();
        standard.setStandardCode(standardCode);

        standardService.insert(standard);
        return SUCCESS_TIP;
    }
    /**
     * 删除标准物质查询
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer standardId) {
        standardService.deleteById(standardId);
        return SUCCESS_TIP;
    }

    /**
     * 修改标准物质查询
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Standard standard,BindingResult bindingResult) {
        standardService.updateById(standard);
        return SUCCESS_TIP;
    }

    /**
     * 标准物质查询详情
     */
    @RequestMapping(value = "/detail/{standardId}")
    @ResponseBody
    public Object detail(@PathVariable("standardId") Integer standardId) {
        return standardService.selectById(standardId);
    }


}
